clear
import excel "./bond_prices.xlsx", sheet("Foglio1") cellrange(A2:AG549) firstrow clear
graph drop _all
cap drop all

* Fill out event dummies
replace xcontrolsdummies =0 if xcontrolsdummies ==.
replace otherbreaks =0 if otherbreaks ==.
replace otherdummies =0 if otherdummies ==.

* Week indicator
gen week = _n
tsset week
labmask week, values(Datesweekendson)

* Choose impulse response horizon
local hmax = 5

* Choose the spread series to be analyzed: 
** New York: mod1SpreadBerlinDawesBD mod2BD mod3BD mod4BD mod1spreadBerlinYoungBY mod2BY mod3BY mod4BY
** London: mod1SpreadBerlinDawesLondon mod2BDL mod3BDL mod4BDL mod1spreadBerlinYoungLondon mod2BYL mod3BYL

* Choose relevant stock market index as control
** New York: SP500CompositePriceIndexw, NYstockexchange40bondsavera 
** London: ProxyforLondonstockindex

* Choose the event type for which LP graphs will be produced
** Exchange Controls: xcontrolsdummies 
** Other strctural breaks: otherbreaks 
** Other events: otherdummies 

*Example: New York, Dawes Bonds, Spread Model 1, Exchange Control events

* Generate LHS variables for the LPs. 

* levels
forvalues h = 0/`hmax' {
	gen spread1_`h' = f`h'.mod1SpreadBerlinDawesBD 
}

* differences
forvalues h = 0/`hmax' {
	gen spread1_d`h' = f`h'.mod1SpreadBerlinDawesBD - l.f`h'.mod1SpreadBerlinDawesBD 
}

* Cumulative
forvalues h = 0/`hmax' {
	gen spread1_c`h' = f`h'.mod1SpreadBerlinDawesBD - l.mod1SpreadBerlinDawesBD 
}

/* Run the LPs */
* Levels
eststo clear
cap drop b u d Weeks Zero
gen Weeks = _n-1 if _n<=`hmax'
gen Zero =  0    if _n<=`hmax'
gen b=0
gen u=0
gen d=0
forv h = 0/`hmax' {
	* levels
	 newey spread1_`h' l(0/4).xcontrolsdummies l(0/4).otherbreaks l(0/4).otherdummies l(1/3).mod1SpreadBerlinDawesBD l(0/4).SP500CompositePriceIndexw, lag(`h')
replace b = _b[xcontrolsdummies]                    if _n == `h'+1
replace u = _b[xcontrolsdummies] + 1.645* _se[xcontrolsdummies]  if _n == `h'+1
replace d = _b[xcontrolsdummies] - 1.645* _se[xcontrolsdummies]  if _n == `h'+1
eststo
}
* nois esttab , se nocons keep(gs1)
gen b_level = b

* Differences
eststo clear
cap drop b u d Weeks Zero
gen Weeks = _n-1 if _n<=`hmax'
gen Zero =  0    if _n<=`hmax'
gen b=0
gen u=0
gen d=0
forv h = 0/`hmax' {
	 newey spread1_d`h' l(0/4).xcontrolsdummies l(0/4).otherbreaks l(0/4).otherdummies  l(1/3).mod1SpreadBerlinDawesBD l(0/4).SP500CompositePriceIndexw, lag(`h')
replace b = _b[xcontrolsdummies]                     if _n == `h'+1
replace u = _b[xcontrolsdummies] + 1.645* _se[xcontrolsdummies]  if _n == `h'+1
replace d = _b[xcontrolsdummies] - 1.645* _se[xcontrolsdummies]  if _n == `h'+1
eststo
}
* nois esttab , se nocons keep(dgs1)
twoway ///
(rarea u d  Weeks,  ///
fcolor(gs13) lcolor(gs13) lw(none) lpattern(solid)) ///
(line b Weeks, lcolor(blue) ///
lpattern(solid) lwidth(thick)) ///
(line Zero Weeks, lcolor(black)), legend(off) ///
title("Impulse response of Spread to XCont event", color(black) size(medsmall)) ///
ytitle("Change", size(medsmall)) xtitle("Week", size(medsmall)) ///
ytitle("Change", size(medsmall)) xtitle("Week", size(medsmall)) ///
graphregion(color(white)) plotregion(color(white))

gr rename fig_diff, replace

* Cumulative
eststo clear
cap drop b u d Weeks Zero
gen Weeks = _n-1 if _n<=`hmax'
gen Zero =  0    if _n<=`hmax'
gen b=0
gen u=0
gen d=0
forv h = 0/`hmax' {
	 newey spread1_c`h' l(0/4).xcontrolsdummies l(0/4).otherbreaks l(0/4).otherdummies l(1/3).mod1SpreadBerlinDawesBD l(0/4).SP500CompositePriceIndexw, lag(`h')
replace b = _b[xcontrolsdummies]                     if _n == `h'+1
replace u = _b[xcontrolsdummies] + 1.645* _se[xcontrolsdummies]  if _n == `h'+1
replace d = _b[xcontrolsdummies] - 1.645* _se[xcontrolsdummies]  if _n == `h'+1
eststo
}
* nois esttab , se nocons keep(dgs1)

twoway ///
(rarea u d  Weeks,  ///
fcolor(gs13) lcolor(gs13) lw(none) lpattern(solid)) ///
(line b Weeks, lcolor(blue) lpattern(solid) lwidth(thick)) ///
(line b_level Weeks, lcolor(red) lpattern(dash) lwidth(vthick)) ///
(line Zero Weeks, lcolor(black)), legend(off) ///
title("Cumulative response to XCont event", color(black) size(medsmall)) ///
subtitle("Levels on levels (solid blue) vs. Cumulative (dash red)", color(black) size(small)) ///
ytitle("Change", size(medsmall)) xtitle("Week", size(medsmall)) ///
graphregion(color(white)) plotregion(color(white))

gr rename fig_cum, replace

gr combine fig_diff fig_cum, ///
graphregion(color(white)) plotregion(color(white)) ///
title("Levels and cumulated impulse responses") ///
note("Note: 90% confidence bands displayed")

